package com.sssre.lttcloud.dict.controller;

import com.sssre.lttcloud.api.entity.DictData;
import com.sssre.lttcloud.common.result.Result;
import com.sssre.lttcloud.dict.service.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典数据控制器
 */
@RestController
@RequestMapping("/dict/data")
@CrossOrigin
public class DictDataController {

    @Autowired
    private DictDataService dictDataService;

    /**
     * 分页查询字典数据
     */
    @GetMapping("/page")
    public Result getDictDataPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String dictType,
            @RequestParam(required = false) String dictLabel) {
        return dictDataService.getDictDataPage(pageNum, pageSize, dictType, dictLabel);
    }

    /**
     * 根据字典类型查询字典数据
     */
    @GetMapping("/type/{dictType}")
    public Result getDictDataByType(@PathVariable String dictType) {
        return dictDataService.getDictDataByType(dictType);
    }

    /**
     * 根据ID查询字典数据
     */
    @GetMapping("/{id}")
    public Result getDictDataById(@PathVariable Long id) {
        return dictDataService.getDictDataById(id);
    }

    /**
     * 新增字典数据
     */
    @PostMapping
    public Result addDictData(@Valid @RequestBody DictData dictData) {
        return dictDataService.addDictData(dictData);
    }

    /**
     * 修改字典数据
     */
    @PutMapping
    public Result updateDictData(@Valid @RequestBody DictData dictData) {
        return dictDataService.updateDictData(dictData);
    }

    /**
     * 删除字典数据
     */
    @DeleteMapping("/{id}")
    public Result deleteDictData(@PathVariable Long id) {
        return dictDataService.deleteDictData(id);
    }

    /**
     * 批量删除字典数据
     */
    @DeleteMapping("/batch")
    public Result deleteDictDatas(@RequestBody List<Long> ids) {
        return dictDataService.deleteDictDatas(ids);
    }

    /**
     * 根据字典类型和字典值查询字典标签
     */
    @GetMapping("/label")
    public Result getDictLabel(
            @RequestParam String dictType,
            @RequestParam String dictValue) {
        return dictDataService.getDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典类型和字典标签查询字典值
     */
    @GetMapping("/value")
    public Result getDictValue(
            @RequestParam String dictType,
            @RequestParam String dictLabel) {
        return dictDataService.getDictValue(dictType, dictLabel);
    }
} 